return
function in Haskell.In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called, known as its return address. The return address is saved, usually on the process's call stack, as part of the operation of making the subroutine call. Return statements in many languages allow a function to specify a return value to be passed back to the code that called the function.
Contents |
In C/C++, return exp;
(where exp
is an expression) is a statement that tells a function to return execution of the program to the calling function, and report the value of exp
. If a function has the return type void, the return statement can be used without a value, in which case the program just breaks out of the current function and returns to the calling one.
In Pascal there is no return statement. A subroutine automatically returns when execution reaches its last executable statement. Values may be returned by assigning to an identifier that has the same name as the subroutine, a function in Pascal terminology. This way the function identifier is used for recursive calls and as result holder. In other languages a user defined result variable is used instead of the function identifier.
Certain programming languages, such as Perl and Ruby, allow the programmer to omit an explicit return statement, specifying instead that the last evaluated expression is the return value of the subroutine. In Python, the value None
is returned when the return statement is omitted.
In Windows PowerShell all evaluated expressions which are not captured (e.g. assigned to a variable, cast to void or piped to $null) are returned from the subroutine as elements in an array, or as a single object in the case that only one object has not been captured.
In Perl, a return value or values of a subroutine can depend on the context in which it was called. The most fundamental distinction is a scalar context where the calling code expects one value, a list context where the calling code expects a list of values and a void context where the calling code doesn't expect any return value at all. A subroutine can check the context using the wantarray
function. A special syntax of return without arguments is used to return an undefined value in scalar context and an empty list in list context. The scalar context can be further divided into Boolean, number, string, and various reference types contexts. Also, a context-sensitive object can be returned using a contextual return sequence, with lazy evaluation of scalar values.
Values returned by the program when it terminates are often captured by batch programs.
Return statements come in many shapes. The following syntaxes are most common:
Language | Return Statement | If value omitted, Return |
---|---|---|
Ada, C, C++, Java, PHP, C#, JavaScript |
return value; |
in C[1] and C++,[2] undefined if function is value-returning |
BASIC |
RETURN
|
|
Lisp |
(return value) |
last statement value |
Perl, Ruby |
return @values; return $value; return; or a contextual return sequence |
last statement value |
Python |
return value
|
None |
Smalltalk |
^ value |
|
Visual Basic .NET |
Return value
|
|
Windows PowerShell |
return value; |
object |
x86 assembly |
ret
|
contents of eax register (by convention) |
Java |
return value; |
Returns Object value. |
It has been argued that one should eschew the use of the explicit return statement except at the textual end of a subroutine, considering that, when it is used to "return early", it may suffer from the same sort of readability problems that are claimed to exist for the GOTO statement. For instance, that in later development, a return statement could be overlooked by a developer, and an action which should be performed at the end of a subroutine (e.g.: a trace statement) might not be performed in all cases. Conversely, it can be argued that using the return statement is worthwhile when the alternative is more convoluted code, harming readability.
Some early implementations of languages such as the original Pascal and C restricted the types that can be returned by a function( e.g., not supporting record or struct types) to simplify their compilers.